package com.tianqu.tms.core.taglib;

import java.io.Serializable;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 
 * @author liugang
 *
 */
public class MenuBean implements Serializable {

	private static final long serialVersionUID = 1;
	/*-------------------- Fields --------------------*/

	private String functionId = "";
	private String parentName = "";
	public String getParentName() {
		return parentName;
	}
	public void setParentName(String parentName) {
		this.parentName = parentName;
	}
	private String menuName = "";
	private String titleImgSrc = "";
	private String targetUri = "";
	private String description = "";
	private boolean needsRole = false;
	private String role = "";
	private List childrenList = new ArrayList();
	private MenuBean parent = null;
	private int level;
	

	/*-------------------- Public methods --------------------*/

	/**
	 * 
	 */
	public void add(MenuBean navigationVO) {
		childrenList.add(navigationVO);
		navigationVO.setLevel(this.level + 1);
		navigationVO.setParent(this);
	}

	/**
	 * 
	 */
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append("name:"+this.getMenuName()+"\n");
		sb.append("level:"+this.getLevel()+"\n");
		for (Iterator iterator = this.childrenList.iterator();iterator.hasNext();) {
			MenuBean navigationVO = (MenuBean) iterator.next();
			if (navigationVO == null) {
				continue;
			}
			sb.append(navigationVO.toString());
		}
		return sb.toString();
	}
	/**
	 * 
	 */
	public String toString2() {
		StringBuffer sb = new StringBuffer();
		if(this.getLevel() == 2)
		{
			sb.append("name:"+this.getMenuName()+"\n");
			sb.append("level:"+this.getLevel()+"\n");
			for (Iterator iterator = this.childrenList.iterator();iterator.hasNext();) {
				MenuBean navigationVO = (MenuBean) iterator.next();
				if (navigationVO == null) {
					continue;
				}
				sb.append(navigationVO.toString());
			}			
		}
		return sb.toString();
	}

	/**
	 * 
	 * @param fromVO
	 */
	public void copy(MenuBean fromVO) {
		this.functionId = fromVO.getFunctionId();
		this.menuName = fromVO.getMenuName();
		this.titleImgSrc = fromVO.getTitleImgSrc();
		this.targetUri = fromVO.getTargetUri();
		this.description = fromVO.getDescription();
		this.needsRole = fromVO.isNeedsRole();
		this.role = fromVO.getRole();
		this.childrenList = fromVO.getChildrenList();
	}

	/**
	 * 
	 * @return
	 */
	public MenuBean getChild() {
		if (!this.childrenList.isEmpty()) {
			return (MenuBean) childrenList.get(0);
		} else {
			return null;
		}
	}
	/**
	 * 
	 */
	public void remove() {
		childrenList.clear();
	}

	/**
	 * 
	 * @param funcId
	 * @return
	 */
	public MenuBean getMenuVO(String funcId) {
		MenuBean navigationVO = null;
		if (compstr(this.functionId, funcId)) {
			return this;
		}

		Iterator iterator = this.childrenList.iterator();
		while (iterator.hasNext()) {
			MenuBean vo = (MenuBean) iterator.next();
			navigationVO = vo.getMenuVO(funcId);
			if (navigationVO != null) {
				break;
			}
		}
		return navigationVO;
	}

	/*-------------------- Getter methods --------------------*/
	public String getFunctionId() {
		return this.functionId;
	}
	public String getMenuName() {
		return this.menuName;
	}
	public String getTitleImgSrc() {
		return this.titleImgSrc;
	}
	public String getTargetUri() {
		return this.targetUri;
	}
	public String getDescription() {
		return this.description;
	}
	public boolean isNeedsRole() {
		return needsRole;
	}
	public List getChildrenList() {
		return this.childrenList;
	}
	public int getChildLevelCount(String sysId) {
		return getChildLevelCount(sysId, 0);
	}

	/**
	 * 
	 * @param sysId
	 * @param count
	 * @return
	 */
	private int getChildLevelCount(String sysId, int count) {
		count++;
		MenuBean navigationVO = null;
		Iterator iterator = this.childrenList.iterator();
		if (this.functionId != null && (compstr(this.functionId, sysId))) {
			return count;
		} else {
			boolean found = false;
			int ret = 0;

			while (iterator.hasNext() && (!found)) {
				navigationVO = (MenuBean) iterator.next();
				ret = 0;

				if (!compstr(this.functionId, sysId)) {
					ret = navigationVO.getChildLevelCount(sysId, count);
					found = (ret != 0);
				}
			}
			return ret;
		}
	}

	/*-------------------- Setter methods --------------------*/

	public void setFunctionId(String funId) {
		this.functionId = funId;
	}
	public void setMenuName(String menuName) {
		this.menuName = menuName;
	}
	public void setTitleImgSrc(String titImgSrc) {
		this.titleImgSrc = titImgSrc;
	}
	public void setTargetUri(String targUri) {
		this.targetUri = targUri;
	}
	public void setDescription(String desc) {
		this.description = desc;
	}
	public void setNeedsRole(boolean b) {
		needsRole = b;
	}
	public void setChildrenList(List childList) {
		this.childrenList = childList;
	}
	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}
	public int getLevel() {
	    if(this.getParent()!= null){
	        level = this.getParent().getLevel() + 1;
	    }else{
	        level = 0;
	    }
		return level;
	}
	public void setLevel(int level) {
		this.level = level;
	}
	public MenuBean getParent() {
		return parent;
	}
	public void setParent(MenuBean parent) {
		this.parent = parent;
	}
	/**
	 * 
	 * @param s1
	 * @param s2
	 * @return
	 */
	protected boolean compstr(String s1, String s2) {
		Collator myCollator = Collator.getInstance();
		boolean b = (myCollator.compare(s1, s2) == 0); 
		return b;
	}
	/**
	 * 
	 * @param vo
	 * @return
	 */
	public boolean isParent(MenuBean vo){
	    if(vo == null) return false;
	    MenuBean tmpParent = vo.getParent();
	    if(tmpParent == null) return false;
	    if(tmpParent.getFunctionId().equals(this.functionId)){
	        return true;
	    }else{
	        return isParent(tmpParent);
	    }
	}
}